home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Taming the Mouse / BlockOut / BlockOut.cs next >
Encoding:
Text File  |  2001-01-15  |  2.5 KB  |  81 lines

  1. //---------------------------------------
  2. // BlockOut.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class BlockOut: Form
  9. {
  10.      bool      bBlocking, bValidBox;
  11.      Point     ptBeg, ptEnd;
  12.      Rectangle rectBox;
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new BlockOut());
  17.      }
  18.      public BlockOut()
  19.      {
  20.           Text = "Blockout Rectangle with Mouse";
  21.           BackColor = SystemColors.Window;
  22.           ForeColor = SystemColors.WindowText;
  23.      }
  24.      protected override void OnMouseDown(MouseEventArgs mea)
  25.      {
  26.           if (mea.Button == MouseButtons.Left)
  27.           {
  28.                ptBeg = ptEnd = new Point(mea.X, mea.Y);
  29.  
  30.                Graphics grfx = CreateGraphics();
  31.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  32.                grfx.Dispose();
  33.  
  34.                bBlocking = true;
  35.           }
  36.      }
  37.      protected override void OnMouseMove(MouseEventArgs mea)
  38.      {
  39.           if (bBlocking)
  40.           {
  41.                Graphics grfx = CreateGraphics();
  42.                grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
  43.                ptEnd = new Point(mea.X, mea.Y);
  44.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  45.                grfx.Dispose();
  46.                Invalidate();
  47.           }
  48.      }
  49.      protected override void OnMouseUp(MouseEventArgs mea)
  50.      {
  51.           if (bBlocking && mea.Button == MouseButtons.Left)
  52.           {
  53.                Graphics grfx = CreateGraphics();
  54.                rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
  55.                grfx.DrawRectangle(new Pen(ForeColor), rectBox);
  56.                grfx.Dispose();
  57.  
  58.                bBlocking = false;
  59.                bValidBox = true;
  60.                Invalidate();
  61.           }
  62.      }
  63.      protected override void OnPaint(PaintEventArgs pea)
  64.      {
  65.           Graphics grfx = pea.Graphics;
  66.  
  67.           if (bValidBox)
  68.                grfx.FillRectangle(new SolidBrush(ForeColor), rectBox);
  69.  
  70.           if (bBlocking)
  71.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  72.      }
  73.      Rectangle Rect(Point ptBeg, Point ptEnd)
  74.      {
  75.           return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
  76.                                Math.Min(ptBeg.Y, ptEnd.Y),
  77.                                Math.Abs(ptEnd.X - ptBeg.X),
  78.                                Math.Abs(ptEnd.Y - ptBeg.Y));
  79.      }
  80. }
  81.